﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AspNetCs.Model;
using AspNetCs.IDAL;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class HappyPet_DAL : IHappyPet
    {
        /// <summary>
        /// 添加宠物乐园
        /// </summary>
        /// <returns></returns>
        public int AddHappyPet(HappyPetModel model)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[11];

            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@PetName";
            paras[0].objValue = model.PetName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@PetPhoto";
            paras[1].objValue = model.PetPhoto;

            paras[2].dbType = SqlDbType.Int;
            paras[2].parName = "@CreateId";
            paras[2].objValue = model.CreateId;

            paras[3].dbType = SqlDbType.DateTime;
            paras[3].parName = "@CreateTime";
            paras[3].objValue = model.CreateTime;

            paras[4].dbType = SqlDbType.VarChar;
            paras[4].parName = "@Content";
            paras[4].objValue = model.Content;


            paras[5].dbType = SqlDbType.Int;
            paras[5].parName = "@IsCheck";
            paras[5].objValue = model.IsCheck;

            paras[6].dbType = SqlDbType.Int;
            paras[6].parName = "@IsDel";
            paras[6].objValue = model.IsDel;

            paras[7].dbType = SqlDbType.Int;
            paras[7].parName = "@AcceeCount";
            paras[7].objValue = model.AcceeCount;

            paras[8].dbType = SqlDbType.Int;
            paras[8].parName = "@TempleId";
            paras[8].objValue = model.TempleId;

            paras[9].dbType = SqlDbType.Int;
            paras[9].parName = "@MusicId";
            paras[9].objValue = model.MusicId;

            paras[10].dbType = SqlDbType.Int;
            paras[10].parName = "@IsIntro";
            paras[10].objValue = model.IsIntro;

            string cmdText = "insert into HappyPet values ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10})";
            cmdText = string.Format(cmdText, paras[0].parName, paras[1].parName, paras[2].parName, paras[3].parName, paras[4].parName, paras[5].parName, paras[6].parName, paras[7].parName, paras[8].parName, paras[9].parName, paras[10].parName);

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }

        /// <summary>
        /// 修改宠物乐园
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateHappPet(HappyPetModel model)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[11];

            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@PetName";
            paras[0].objValue = model.PetName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@PetPhoto";
            paras[1].objValue = model.PetPhoto;

            paras[2].dbType = SqlDbType.Int;
            paras[2].parName = "@CreateId";
            paras[2].objValue = model.CreateId;

            paras[3].dbType = SqlDbType.DateTime;
            paras[3].parName = "@CreateTime";
            paras[3].objValue = model.CreateTime;

            paras[4].dbType = SqlDbType.VarChar;
            paras[4].parName = "@Content";
            paras[4].objValue = model.Content;


            paras[5].dbType = SqlDbType.Int;
            paras[5].parName = "@IsCheck";
            paras[5].objValue = model.IsCheck;

            paras[6].dbType = SqlDbType.Int;
            paras[6].parName = "@IsDel";
            paras[6].objValue = model.IsDel;

            paras[7].dbType = SqlDbType.Int;
            paras[7].parName = "@AcceeCount";
            paras[7].objValue = model.AcceeCount;

            paras[8].dbType = SqlDbType.Int;
            paras[8].parName = "@TempleId";
            paras[8].objValue = model.TempleId;

            paras[9].dbType = SqlDbType.Int;
            paras[9].parName = "@MusicId";
            paras[9].objValue = model.MusicId;

            paras[10].dbType = SqlDbType.Int;
            paras[10].parName = "@Id";
            paras[10].objValue = model.Id;

            string cmdText = "update HappyPet set PetName={0},PetPhoto={1},CreateId={2},CreateTime={3},Content={4},IsCheck={5},IsDel={6},AcceeCount={7},TempleId={8},MusicId={9} where Id={10}";
            cmdText = string.Format(cmdText, paras[0].parName, paras[1].parName, paras[2].parName, paras[3].parName, paras[4].parName, paras[5].parName, paras[6].parName, paras[7].parName, paras[8].parName, paras[9].parName, paras[10].parName);

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }

        /// <summary>
        /// 获得我创建我宠物乐园
        /// </summary>
        /// <param name="queryItems">查询的字段</param>
        /// <param name="CreateId">创建者编号</param>
        /// <returns></returns>
        public DataTable getMyCreateHappyPet(string queryItems, int CreateId)
        {
            DataTable dataTable = new DataTable();
            string cmdText = "";
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@CreateId";
            paras[0].objValue = CreateId;
            cmdText = "select " + queryItems + " from HappyPet where CreateId=@CreateId and IsDel=0";
            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText, paras);


            return dataTable;
        }




        /// <summary>
        /// 通过编号查询宠物乐园详细信息
        /// </summary>
        /// <param name="queryItems">查询的字段</param>
        /// <param name="Id">编号</param>
        /// <returns></returns>
        public DataTable getHappyPetById(string queryItems, int Id)
        {
            DataTable dataTable = new DataTable();
            string cmdText = "";
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@Id";
            paras[0].objValue = Id;
            cmdText = "select " + queryItems + " from HappyPet where Id=@Id";
            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText, paras);
            return dataTable;
        }

        /// <summary>
        /// 删除宠物乐园
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DeleteHappyPet(int Id)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@Id";
            paras[0].objValue = Id;

            string cmdText = "update HappyPet set IsDel=1 where Id=@Id";
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 0表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            int count = 0;
            string cmdText = "update HappyPet set IsIntro=@IsIntro where id=@id";
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@IsIntro";
            paras[0].objValue = CancleOrIntro;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@id";
            paras[1].objValue = Id;

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }

        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            int count = 0;
            string cmdText = "update HappyPet set IsCheck=@IsCheck where id=@id";
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@IsCheck";
            paras[0].objValue = CancleOrIntro;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@id";
            paras[1].objValue = Id;

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }


        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            string cmdText = "update HappyPet set MusicId=@MusicId where Id=@ID";
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[2];

            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@MusicId";
            paras[0].objValue = MusicId;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@ID";
            paras[1].objValue = Id;

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);
            return count;

        }

        public void filterUpdate(string Upfilter, string filter)
        {
            db Conn = new db();
            if (filter + "" != "")
            {
                filter = "  where " + filter;
            }
            Conn.Execute_Noget("update HappyPet Set " + Upfilter + " " + filter);
            Conn.close();
        }
        public HappyPetModel GetModel(DataRow dr)
        {
            HappyPetModel Md = new HappyPetModel();

            if (dr["Id"] != null)
            {
                Md.Id = StringHelper.isNumber_int(dr["Id"]);
            }

            if (dr["PetName"] != null)
            {
                Md.PetName = dr["PetName"].ToString();
            }

            if (dr["PetPhoto"] != null)
            {
                Md.PetPhoto = dr["PetPhoto"].ToString();
            }

            if (dr["CreateId"] != null)
            {
                Md.CreateId = StringHelper.isNumber_int(dr["CreateId"]);
            }

            if (dr["CreateTime"] != null)
            {
                Md.CreateTime = StringHelper.isDatetime(dr["CreateTime"]);
            }

            if (dr["Content"] != null)
            {
                Md.Content = dr["Content"].ToString();
            }
            if (dr["IsCheck"] != null)
            {
                Md.IsCheck = StringHelper.isNumber_int(dr["IsCheck"]);
            }
            if (dr["IsDel"] != null)
            {
                Md.IsDel = StringHelper.isNumber_int(dr["IsDel"]);
            }

            if (dr["AcceeCount"] != null)
            {
                Md.AcceeCount = StringHelper.isNumber_int(dr["AcceeCount"]);
            }

            if (dr["TempleId"] != null)
            {
                Md.TempleId = StringHelper.isNumber_int(dr["TempleId"]);
            }
            if (dr["MusicId"] != null)
            {
                Md.MusicId = StringHelper.isNumber_int(dr["MusicId"]);
            }

            if (dr["IsIntro"] != null)
            {
                Md.IsIntro = StringHelper.isNumber_int(dr["IsIntro"]);
            }














            return Md;
        }


        public DataTable GetDatatable(string filter)
        {

            DataTable dataTable = new DataTable();
            string cmdText = "";
            string wherestr = "";

            if (filter + "" != "")
            {
                wherestr += "  where " + filter;
            }
            cmdText = "select * from HappyPet " + wherestr;
            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText);
            return dataTable;

        }
    }
}
